package humynn.engine
{
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import flashx.textLayout.formats.TextAlign;

	/**
	 * The Label class is used to create visible text objects.
	 */
	public class Label extends Control
	{
		private var _textField:TextField;
		private var _bmpData:BitmapData;
		
		/**
		 * Creates a new label object and sets the default values.
		 * 
		 * @param x The x location on the Stage.
		 * @param y The y location on the Stage.
		 */
		public function Label(x:int = 0, y:int = 0)
		{
			super();
			
			var textFormat:TextFormat = new TextFormat();
			textFormat.color = 0xFFFFFFFF;
			textFormat.font = "Verdana";
			textFormat.size = 40;
			
			_textField = new TextField();
			_textField.autoSize = TextFieldAutoSize.LEFT;
			_textField.defaultTextFormat = textFormat;
			
			_rect = new Rectangle();
			_rect.x = x;
			_rect.y = y;
		}
		
		/**
		 * Returns the text that is displayed.
		 * 
		 * @returns The displayed text.
		 */
		public function get text():String
		{
			return _textField.text;
		}
		
		/**
		 * Sets the text that is displayed, and redraws the BitmapData.
		 * 
		 * @param value The text to be rendered.
		 */
		public function set text(value:String):void
		{
			if(value == null)
			{
				return;
			}
			
			_textField.text = value;
			redrawBitmapData();
		}
		
		/**
		 * Gets the width of the label.
		 * 
		 * @returns The width of the label.
		 */
		public function get width():uint
		{
			return _bmpData.rect.width;
		}
		
		/**
		 * Sets the width of the label.
		 * 
		 * @param value The new width of the label.
		 */
		public function set width(value:uint):void
		{
			_textField.wordWrap = true;
			_textField.width = value;
			redrawBitmapData();
		}
		
		/**
		 * Gets the height of the label.
		 * 
		 * @returns The height of the label.
		 */
		public function get height():uint
		{
			return _bmpData.rect.height;
		}
		
		/**
		 * Sets the height of the label.
		 * 
		 * @param value The new height of the label.
		 */
		public function set height(value:uint):void
		{
			_textField.wordWrap = true;
			_textField.height = value;
			redrawBitmapData();
		}
		
		/**
		 * Turns word wrapping on and off.
		 * 
		 * @param value On (true) or off (false)
		 */
		public function set wordWrap(value:Boolean):void
		{
			_textField.wordWrap = value;
			redrawBitmapData();
		}
		
		/**
		 * Sets the color of the text.
		 * 
		 * @param value The new color of the text.
		 */
		public function set color(value:uint):void
		{
			_textField.textColor = value;
			redrawBitmapData();
		}
		
		/**
		 * Sets the size of the text.
		 * 
		 * @param value The new size of the text.
		 */
		public function set fontSize(value:uint):void
		{
			var textFormat:TextFormat = _textField.getTextFormat();
			textFormat.size = value;
			_textField.defaultTextFormat = textFormat;
			redrawBitmapData();
		}
		
		/**
		 * Changes the font face or style of font.
		 * 
		 * @param value The new font face to use.
		 */
		public function set fontFace(value:String):void
		{
			var textFormat:TextFormat = _textField.getTextFormat();
			textFormat.font = value;
			_textField.defaultTextFormat = textFormat;
			redrawBitmapData();
		}
		
		/**
		 * Sets the text alignment.
		 * 
		 * @param value The new text alignment.
		 */
		public function set align(value:String):void
		{
			var textFormat:TextFormat = _textField.getTextFormat();
			textFormat.align = value;
			_textField.defaultTextFormat = textFormat;
			redrawBitmapData();
		}
		
		/**
		 * Renders the text to a bitmap so that it can be drawn.
		 */
		private function redrawBitmapData():void
		{
			if(_bmpData != null)
			{
				_bmpData.dispose();
			}
			
			_bmpData = new BitmapData(_textField.width, _textField.height, true, 0x00000000);
			_bmpData.draw(_textField);
		}
		
		/**
		 * Draws the BitmapData.
		 * 
		 * @param renderer The renderer to draw to.
		 */
		public override function draw(renderer:Renderer):void
		{
			renderer.drawBmp(_bmpData, _rect.x, _rect.y);
		}
	}
}